﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VirtualRouterClient;
using VirtualRouterClient.VirtualRouterService;

namespace Wpf_VirtualRouterPlus.UserControls
{
    /// <summary>
    /// Interaction logic for PeersControl.xaml
    /// </summary>
    public partial class PeerManager : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }

        ObservableCollection<PeerDevice> _ocPeer;

        public ObservableCollection<PeerDevice> OcPeer
        {
            get { return _ocPeer; }
            set { _ocPeer = value;
            OnPropertyChanged("OcPeer");
            }
        }


        public PeerManager()
        {
            InitializeComponent();
        }




        private void _removeDisconnectedPeers(ConnectedPeer[] peers)
        {
            List<PeerDevice> peersToRemove = new List<PeerDevice>();

            foreach (var element in lvPeer.Items)
            {
                var elem = element as PeerDevice;
                if (elem != null)
                {
                    var exists = false;
                    foreach (var p in peers)
                    {
                        if (p.MacAddress.ToLowerInvariant() == elem.Peer.MacAddress.ToLowerInvariant())
                        {
                            exists = true;
                            break;
                        }
                    }
                    if (!exists)
                    {
                        peersToRemove.Add(elem);
                    }
                }
            }
            foreach (var elem in peersToRemove)
            {
                lvPeer.Items.Remove(elem);
            }
        }



        private bool isPeerAlreadyConnected(ConnectedPeer peer)
        {
            foreach (var element in lvPeer.Items)
            {
                var elem = element as PeerDevice;
                if (elem != null)
                {
                    if (elem.Peer.MacAddress.ToLowerInvariant() == peer.MacAddress.ToLowerInvariant())
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        internal void callBack_UpdatePeers()
        {
            App myApp = (App)App.Current;
            if (myApp.IsVirtualRouterServiceConnected)
            {
                //panelConnections.Children.Clear();

                var peers = myApp.VirtualRouter.GetConnectedPeers();

                lblNumOfPeers.Content = peers.Count().ToString();

                foreach (var p in peers)
                {
                    if (!this.isPeerAlreadyConnected(p))
                    {
                        lvPeer.Items.Add(new PeerDevice(p));
                    }
                }
                _removeDisconnectedPeers(peers);
            }
            else
            {
                lblNumOfPeers.Content = "0";
            }
        }
    }
}
